//
//  WeeklyLetterItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/22.
//

import SwiftUI
import Kingfisher

//来电记录
struct CallHistoryItemView: View {
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = CallHistoryModel()
    @State var item: CallHistoryBean
    @State private var leftViewHeight : CGFloat = 130

    var body: some View {
        HStack {
            VStack(alignment:.leading,spacing: 0){
                Text(TimeUtils.getDate(time: item.time, format: "yyyy"))
                    .font(.system(size: 12))
                    .foregroundStyle(.bgNormalText3)
                Text(TimeUtils.getDate(time: item.time, format: "M月d号 H:mm"))
                    .font(.system(size: 16))
                    .foregroundStyle(.bgNormalText)
                    .padding(.top,5)
                Spacer()
                HStack(spacing:5){
                    KFImage(URL(string: item.aiRole?.avatar.formatCdn() ?? ""))
                        .resizable()
                        .placeholder {
                            Image(.appDefaultLogo)
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .padding(4)
                                .background(.bgItemGray)
                        }
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 20,height: 20)
                        .clipShape(Circle())
                    Text(item.aiRole?.name ?? "")
                        .font(.system(size: 12))
                        .foregroundStyle(.gray)
                }
            }
            .padding(.vertical,15)
            .padding(.horizontal,15)
            Spacer()
            ZStack {
                Image(.homeIconListen)
                    .resizable()
                    .renderingMode(.template)
                    .foregroundColor(themeManager.themeColor)
                    .frame(width: 40, height: 40)
            }.frame(width: leftViewHeight * 1.2, height: leftViewHeight)
                .background(themeManager.themeColor.opacity(0.3))
                .cornerRadius(20)
        }.frame(maxWidth: .infinity,maxHeight: leftViewHeight)
            .background(.bgItem)
            .cornerRadius(20)
            .contentShape(Rectangle())
            .onTapGesture {
                viewModel.doVoice(bean: item)
            }
    }
}


//#Preview {
//    CallHistoryItemView(item: WeeklyLetterBean())
//}


//定义预览提供者
struct CallHistoryItemView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: CallHistoryItemView(item: CallHistoryBean()))
    }
}
